<?php
namespace backend\modules\business\controllers;

use backend\controllers\BaseController;
use common\models\BusSupplier;
use common\models\BusSupplieFinance;
use yii\base\Object;
use yii\helpers\Url;
use yii\data\Pagination;
use common\extended\system\Util;

/**
 * 供应商管理
 * @author     chenxiaolin
 * @date        2016年2月19日
 * @version     2.0.0
 * @copyright   Copyright 2016 meelier.com
 */
class SupplierController extends BaseController{
    public $layout = '/agent';
    /**
     * Index
     * @date: 2016年3月31日 
     * @author: chenxiaolin
     */
    public function actionIndex(){
        $this->redirect(Url::toRoute("supplier/list"));
    }
    /**
     * 供应商列表
     * @date: 2016年2月19日 
     * @author: chenxiaolin
     */
    public function actionList(){
        $userInfo = $this->userInfo;
        $query = BusSupplier::find()->where('boss_id = :id and del_flag = 1',[':id' => $userInfo->uid]);
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count(),'pageSize' => $this->pageNavLimit]);
        $data = $query->offset($pages->offset)->limit($pages->limit)->all();
        return $this->render('list',['data' => $data,'page' => $pages]);
    }
    /**
     * 添加供应商
     * @date: 2016年2月19日 
     * @author: chenxiaolin
     */
    public function actionAdd(){
        if(!$this->request->isPost){
            return $this->render('add');
        }
        $userInfo = $this->userInfo;
        //获取提交数据
        $supplier_name = $this->request->post('supplier_name','');
        $supplier_contact = $this->request->post('supplier_contact','');
        $supplier_phone = $this->request->post('supplier_phone','');
        $supplier_remark = $this->request->post('supplier_remark','');
        $supplier_balance = $this->request->post('supplier_balance',0);
        $supplier_give_balance = $this->request->post('supplier_give_balance',0);
        if($supplier_name == ''){
            $this->error('供应商名字不能为空！');
        }
        if(!empty($supplier_phone)){
            if(!Util::checkMobile($supplier_phone)){
                $this->error('电话号码错误！');
            }
        }
        /* var_dump(is_numeric($supplier_give_balance));
        exit; */
        /* var_dump(strlen($supplier_balance) > 6);
        exit; */
        if(!empty($supplier_balance)){
            if(!is_numeric($supplier_balance) ||  strlen($supplier_balance) > 6 ){
                $this->error('货款为数字类型且最长6位数！');
            }
        }
        if(!empty($supplier_give_balance)){
            if(!is_numeric($supplier_give_balance) || strlen($supplier_give_balance) > 6){
                $this->error('赠送为数字类型且最长6位数！');
            }
        }
        if(empty($supplier_balance)){
            $supplier_balance = 0;
        }
        if(empty($supplier_give_balance)){
            $supplier_give_balance = 0;
        }
        
        if(Util::utf8_strlen($supplier_remark) > 30){
            $this->error('备注信息不超过30字！');
        }
        if(Util::utf8_strlen($supplier_name) > 30){
            $this->error('供应商名称不超过30字！');
        }
        if(Util::utf8_strlen($supplier_contact) > 5){
            $this->error('负责人不超过5字！');
        }
        //开启事务
        $transaction = \Yii::$app->db->beginTransaction();
        try {
            //保存供应商信息
            $supplierModel = new BusSupplier();
            $supplierModel->supplier_name = $supplier_name;
            $supplierModel->supplier_contact = $supplier_contact;
            $supplierModel->supplier_phone = $supplier_phone;
            $supplierModel->supplier_balance = $supplier_balance;
            $supplierModel->supplier_give_balance = $supplier_give_balance;
            $supplierModel->supplier_remark = $supplier_remark;
            $supplierModel->boss_id = $userInfo->boss_uid;
            $supplierModel->shop_id = $userInfo->shop_id;
            $supplierModel->del_flag = 1;
            $rsSupplier = $supplierModel->save(false);
            //添加供应商财务日志
            $finance = new BusSupplieFinance();
            $finance->supplier_id = $supplierModel->supplier_id;
            $finance->finance_type = '加货款';
            $finance->create_date = date('Y-m-d H:i:s');
            $finance->finance_money = $supplier_balance;
            $finance->finance_give_money = $supplier_give_balance;
            $finance->finance_balance = $supplier_balance;
            $finance->finance_give_balance = $supplier_give_balance;
            $finance->finance_remark = '';
            $finance->shop_id = $userInfo->shop_id;
            $finance->boss_id = $userInfo->boss_uid;
            $rsFinance = $finance->save(false);
            //提交
            $transaction->commit();
        } catch (\Exception $e) {
            //回滚
            $transaction->rollBack();
            $this->error('添加失败！');
        }
        $this->redirect(Url::toRoute("supplier/list"));
        
    }
    /**
     * 编辑供应商
     * @date: 2016年2月22日 
     * @author: chenxiaolin
     */
    public function actionEdit(){
        //获取数据
        $supplier_id = intval($this->request->post('supplier_id',0));
        $supplier_name = $this->request->post('supplier_name','');
        $supplier_contact = $this->request->post('supplier_contact','');
        $supplier_phone = $this->request->post('supplier_phone','');
        $supplier_remark = $this->request->post('supplier_remark','');
        if($supplier_id <= 0){
            $this->error('参数错误！');
        }
        if($supplier_name == ''){
            $this->error('请输入供应商名称！');
        }
        if(!empty($supplier_phone)){
            if(!Util::checkMobile($supplier_phone)){
                $this->error('请输入正确的手机号！');
            }
        }
        if(Util::utf8_strlen($supplier_remark) > 30){
            $this->error('备注信息不超过30字！');
        }
        if(Util::utf8_strlen($supplier_name) > 30){
            $this->error('供应商名称不超过30字！');
        }
        if(Util::utf8_strlen($supplier_contact) > 5){
            $this->error('负责人不超过5字！');
        }
        $supplier = BusSupplier::find()->where('supplier_id = :id and del_flag = 1',[':id'=>$supplier_id])->one();
        if(!$supplier){
            $this->error('该供应商不存在！');
        }
        //修改数据
        $supplier->supplier_name = $supplier_name;
        $supplier->supplier_contact = $supplier_contact;
        $supplier->supplier_phone = $supplier_phone;
        $supplier->supplier_remark = $supplier_remark;
        $rs = $supplier->save(false);
        if($rs){
            $this->redirect(Url::toRoute("supplier/list"));
        }else{
            $this->error("修改失败！");
        }
    }
    /**
     * 删除供应商
     * @date: 2016年2月22日 
     * @author: chenxiaolin
     */
    public function actionDel(){
        $id = intval($this->request->post('id',0));
        if($id <= 0){
            $this->error('参数错误！');
        }
        $supplier = BusSupplier::findOne($id);
        if(!$supplier){
            $this->error('该供应商不存在！');
        }
        $supplier->del_flag = -1;
        if($supplier->save(false)){
            $arr=[
             'status'=>10000
            ];
            echo json_encode($arr);
            //$this->redirect(Url::toRoute("supplier/list"));
        }else{
            $this->error('操作失败！');
        }
    }
}










